package org.aurora.common.util;

public class ThreadSleepTimer {
    private long preTimeMillis = System.currentTimeMillis();

    private final long awaitTimeMillis;

    public ThreadSleepTimer(long awaitTimeMillis) {
        this.awaitTimeMillis = awaitTimeMillis;
    }

    public void await() throws InterruptedException {
        long currentTimeMillis = SystemTimeUtil.currentTimeMillis();

        long sleepTimeMillis = this.awaitTimeMillis - (currentTimeMillis - this.preTimeMillis);

        if (sleepTimeMillis > 0) {
            Thread.sleep(sleepTimeMillis);
        }

        this.preTimeMillis = SystemTimeUtil.currentTimeMillis();
    }

    public void update() {
        this.preTimeMillis = SystemTimeUtil.currentTimeMillis();
    }
}
